﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ElevatorLib
{
    /// <summary>
    /// 仿真系统配置类
    /// </summary>
    public class SimulationConfig:XMLBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="url">配置文件</param>
        public SimulationConfig(string url)
            : base(url)
        {
 
        }

        /// <summary>
        /// 判断配置文件是否存在
        /// </summary>
        /// <returns></returns>
        public bool IsConfigFileExist()
        {
            return isExist();
        }

        /// <summary>
        /// 更新楼层数配置
        /// </summary>
        /// <param name="num">楼层数</param>
        /// <returns></returns>
        public bool UpdateFloorNum(int num)
        {
            return Update("/Root/Building/FloorNum", "", num.ToString());
        }

        /// <summary>
        /// 读取楼层数配置
        /// </summary>
        /// <returns>楼层数</returns>
        public int ReadFloorNum()
        {
            return Convert.ToInt32(Read("/Root/Building/FloorNum", ""));
        }

        /// <summary>
        /// 更新各楼层高度
        /// </summary>
        /// <param name="heights">楼层高度</param>
        /// <returns></returns>
        public bool UpdateFloorHeight(double[] heights)
        {
            int num = ReadFloorNum();
            if (heights.Length != num)
            {
                return false;
            }
            string str = heights[0].ToString();
            for (int i = 1; i < heights.Length; i++)
            {
                str +="-"+ heights[i];
            }
            return Update("/Root/Building/FloorHeight", "", str);
        }

        /// <summary>
        /// 读取各楼层高度
        /// </summary>
        /// <returns>楼层高度</returns>
        public double[] ReadFloorHeight()
        {
            string str = Read("/Root/Building/FloorHeight", "");
            string[] strs = str.Split('-');
            double[] heights = new double[strs.Length];
            for (int i = 0; i < strs.Length; i++)
            {
                try
                {
                    heights[i] = Convert.ToDouble(strs[i]);
                }
                catch
                {
                    return null;
                }
            }
            return heights;
        }

        /// <summary>
        /// 更新电梯数量配置
        /// </summary>
        /// <param name="num">电梯数量</param>
        /// <returns></returns>
        public bool UpdateElevatorNum(int num)
        {
            return Update("/Root/Building/ElevatorNum", "", num.ToString());
        }

        /// <summary>
        /// 读取电梯数量配置
        /// </summary>
        /// <returns>电梯数量</returns>
        public int ReadElevatorNum()
        {
            return Convert.ToInt32(Read("/Root/Building/ElevatorNum", ""));
        }

        /// <summary>
        /// 更新轿厢容量配置
        /// </summary>
        /// <param name="num">轿厢容量</param>
        /// <returns></returns>
        public bool UpdateContainer(int num)
        {
            return Update("/Root/Elevator/Container", "", num.ToString());
        }

        /// <summary>
        /// 读取轿厢容量配置
        /// </summary>
        /// <returns>轿厢容量</returns>
        public int ReadContainer()
        {
            return Convert.ToInt32(Read("/Root/Elevator/Container", ""));
        }


        /// <summary>
        /// 更新电梯额定速度配置
        /// </summary>
        /// <param name="v">电梯额定速度</param>
        /// <returns></returns>
        public bool UpdateVelocity(double v)
        {
            return Update("/Root/Elevator/Velocity", "", v.ToString());
        }

        /// <summary>
        /// 读取电梯额定速度配置
        /// </summary>
        /// <returns>电梯额定速度</returns>
        public double ReadVelocity()
        {
            return Convert.ToDouble(Read("/Root/Elevator/Velocity", ""));
        }

        /// <summary>
        /// 更新电梯开关门时间配置
        /// </summary>
        /// <param name="t">电梯开关门时间</param>
        /// <returns></returns>
        public bool UpdateOpenClose(double t)
        {
            return Update("/Root/Elevator/OpenClose", "", t.ToString());
        }

        /// <summary>
        /// 读取电梯开关门时间配置
        /// </summary>
        /// <returns>电梯开关门时间</returns>
        public double ReadOpenClose()
        {
            return Convert.ToDouble(Read("/Root/Elevator/OpenClose", ""));
        }

        /// <summary>
        /// 更新电梯运行行为配置
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="className">类名</param>
        /// <returns></returns>
        public bool UpdateRunningBehavior(string fileName, string className)
        {
            return Update("/Root/Elevator/RunningBehavior", "FileName", fileName) && Update("/Root/Elevator/RunningBehavior", "ClassName", className);
        }

        /// <summary>
        /// 读取电梯运行行为配置
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="className">类名</param>
        public void ReadRunningBehavior(out string fileName, out string className)
        {
            fileName = Read("/Root/Elevator/RunningBehavior", "FileName");
            className = Read("/Root/Elevator/RunningBehavior", "ClassName");
        }

        /// <summary>
        /// 更新客流生成器配置
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="className">类名</param>
        /// <returns></returns>
        public bool UpdatePersonCreator(string fileName, string className)
        {
            return Update("/Root/PersonCreator", "FileName", fileName) && Update("/Root/PersonCreator", "ClassName", className);
        }

        /// <summary>
        /// 读取客流生成器配置
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="className">类名</param>
        public void ReadPersonCreator(out string fileName, out string className)
        {
            fileName = Read("/Root/PersonCreator", "FileName");
            className = Read("/Root/PersonCreator", "ClassName");
        }

        /// <summary>
        /// 更新群控策略配置
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="className">类名</param>
        /// <returns></returns>
        public bool UpdateStrategy(string fileName,string className)
        {
            return Update("/Root/Strategy", "AlgorithmFileName", fileName) && Update("/Root/Strategy", "AlgorithmName", className);
        }

        /// <summary>
        /// 读取群控策略配置
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="className">类名</param>
        public void ReadStrategy(out string fileName, out string className)
        {
            fileName = Read("/Root/Strategy", "AlgorithmFileName");
            className = Read("/Root/Strategy", "AlgorithmName");
        }

        /// <summary>
        /// 更新分析工具集配置
        /// </summary>
        /// <param name="folderName">分析工具集文件夹</param>
        /// <returns></returns>
        public bool UpdateAnalysis(string folderName)
        {
            return Update("/Root/Analysis", "AnalysisFolderName", folderName);
        }

        /// <summary>
        /// 读取分析工具集配置
        /// </summary>
        /// <returns>分析工具集文件夹</returns>
        public string ReadAnalysis()
        {
            return Read("/Root/Analysis", "AnalysisFolderName");
        }

    }
}
